package com.xbongbong.crm.perform.query.render.insert;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmNewPageRenderService;
import com.xbongbong.crm.handle.explain.impl.CustomerExplainHandleImpl;
import com.xbongbong.crm.help.FineClueHelp;
import com.xbongbong.crm.help.PhoneHelp;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.model.FineClueCompanyModel;
import com.xbongbong.paas.pojo.dto.FormDataAddGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddGetVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.domain.entity.FineClueCompanyEntity;
import com.xbongbong.pro.domain.entity.FormConfigEntity;
import com.xbongbong.pro.enums.FineClueEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.FormConfigEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.help.DefaultAttrHelp;
import com.xbongbong.saas.model.FormConfigModel;
import com.xbongbong.saas.model.FormTemplateModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/18 19:26
 */
@Service("customerNewPageRenderServiceImpl")
public class CustomerNewPageRenderServiceImpl extends AbstractCrmNewPageRenderService {



    @Resource
    private CustomerExplainHandleImpl customerExplainHandle;
    @Resource
    private PhoneHelp phoneHelp;
    @Resource
    private FormTemplateModel formTemplateModel;
    @Resource
    private FineClueHelp fineClueHelp;


    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
    }

    @Override
    public void businessHandleExplain(FormDataAddGetDTO formDataAddGetDTO, List<FieldAttrEntity> explainList, PaasFormEntityExt paasFormEntityExt) throws XbbException {
        String corpid = formDataAddGetDTO.getCorpid();
        Long formId = formDataAddGetDTO.getFormId();

        phoneHelp.handlerCustomerRepeat(corpid, formId, explainList, formDataAddGetDTO.getBusinessType());

        // 各个业务场景，只有一个号码新建，直接break
        for (FieldAttrEntity item : explainList) {
             if (Objects.equals(item.getAttr(), CustomerManagementEnum.PHONE.getAttr())) {
                if ( Objects.nonNull(formDataAddGetDTO.getSaasSpecialParamPojo()) && formDataAddGetDTO.getSaasSpecialParamPojo().getPhoneNum() != null) {
                    // 呼叫中心带过来的号码
                    JSONArray phoneArray = new JSONArray();
                    JSONObject phoneObject = new JSONObject();
                    phoneObject.put(StringConstant.PHONE_TYPE_ATTR, "1");
                    phoneObject.put(StringConstant.PHONE_TEL_ATTR, formDataAddGetDTO.getSaasSpecialParamPojo().getPhoneNum());
                    phoneArray.add(phoneObject);
                    item.setDefaultAttr(DefaultAttrHelp.getDefaultAttrPojo(phoneArray));
                }
            }
        }
        FormatExplainDTO formatExplainDTO = new FormatExplainDTO();
        BeanUtil.copyProperties(formDataAddGetDTO, formatExplainDTO);
        formatExplainDTO.setExplainList(explainList);
        if (Objects.nonNull(formDataAddGetDTO.getSaasSpecialParamPojo())) {
            formatExplainDTO.setLinkBusinessType(formDataAddGetDTO.getSaasSpecialParamPojo().getLinkBusinessType());
        }
        explainList = customerExplainHandle.newHandleExplain(formatExplainDTO);
        // 设置模板字段
        formTemplateModel.setTemplate(corpid, paasFormEntityExt, formDataAddGetDTO.getBusinessType(), formDataAddGetDTO.getLoginUser(), explainList, 1);

    }


    @Override
    public void formatSaasAttrToReturn(FormDataAddGetDTO formDataAddGetDTO, FormDataAddGetVO formDataAddGetVO, PaasFormEntityExt paasFormEntityExt) throws XbbException {
        // 查看是否能使用精线索的工商查询
        if (fineClueHelp.fineClueUsable(formDataAddGetDTO.getCorpid(), FineClueEnum.BUSINESS)) {
            formDataAddGetVO.setIsFineClue(1);
        }
        formDataAddGetVO.setPaasFormEntity(paasFormEntityExt);
    }
}
